/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.polyak.iconswitch.sample;

import com.polyak.iconswitch.IconSwitch;
import com.polyak.iconswitch.sample.utils.ResUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.element.ShapeElement;
import ohos.utils.net.Uri;


/**
 * 主页面Ability
 */
public class MainAbility extends Ability implements
        Component.ClickedListener,
        IconSwitch.CheckedChangeListener {
    private Uri urlGithubPolyak;
    private Uri urlGithubYarolegovich;
    private Uri urlDribbbleProkhoda;

    private IconSwitch iconSwitch;
    private Component title;
    private Component scrollView;
    private Component content;

    private Component item1;
    private Component item2;
    private Component item3;

    private int[] toolbarColors;
    private int[] statusBarColors;

    private AnimatorProperty contentAnim;
    private int scrollHeight;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        initPageView();
        initPageListener();
        process();
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        if (contentAnim != null) {
            contentAnim = null;
        }
    }

    private void initPageView() {
        urlGithubPolyak = Uri.parse(getString(ResourceTable.String_url_1));
        urlGithubYarolegovich = Uri.parse(getString(ResourceTable.String_url_2));
        urlDribbbleProkhoda = Uri.parse(getString(ResourceTable.String_url_3));
        Component is = findComponentById(ResourceTable.Id_is_switch);
        if (is instanceof IconSwitch) {
            iconSwitch = (IconSwitch) is;
        }
        title = findComponentById(ResourceTable.Id_dl_main_title);
        scrollView = findComponentById(ResourceTable.Id_dl_main_scroll);
        content = findComponentById(ResourceTable.Id_dl_main_content);
        item1 = findComponentById(ResourceTable.Id_dl_main_content_item1);
        item2 = findComponentById(ResourceTable.Id_dl_main_content_item2);
        item3 = findComponentById(ResourceTable.Id_dl_main_content_item3);
        scrollHeight = ResUtils.getWindowHeight(getContext())-title.getHeight();
    }

    private void initPageListener() {
        item1.setClickedListener(this);
        item2.setClickedListener(this);
        item3.setClickedListener(this);
        iconSwitch.setCheckedChangeListener(this);
    }

    private void process() {
        initColors();
        updateColors();
    }

    private void initColors() {
        toolbarColors = new int[IconSwitch.Checked.values().length];
        statusBarColors = new int[toolbarColors.length];
        toolbarColors[IconSwitch.Checked.LEFT.ordinal()] =
                ResUtils.getColorFromRes(this, ResourceTable.Color_informationPrimary).getValue();
        statusBarColors[IconSwitch.Checked.LEFT.ordinal()] =
                ResUtils.getColorFromRes(this, ResourceTable.Color_informationPrimaryDark).getValue();
        toolbarColors[IconSwitch.Checked.RIGHT.ordinal()] =
                ResUtils.getColorFromRes(this, ResourceTable.Color_mapPrimary).getValue();
        statusBarColors[IconSwitch.Checked.RIGHT.ordinal()] =
                ResUtils.getColorFromRes(this, ResourceTable.Color_mapPrimaryDark).getValue();
    }

    @Override
    public void onClick(Component cpt) {
        switch (cpt.getId()) {
            case ResourceTable.Id_dl_main_content_item1:
                open(urlGithubPolyak);
                break;
            case ResourceTable.Id_dl_main_content_item2:
                open(urlGithubYarolegovich);
                break;
            case ResourceTable.Id_dl_main_content_item3:
                open(urlDribbbleProkhoda);
                break;
        }
    }

    @Override
    public void onCheckChanged(IconSwitch.Checked current) {
        updateColors();
        if (contentAnim == null) {
            contentAnim = content.createAnimatorProperty();
        }


        float start = current == IconSwitch.Checked.LEFT ? scrollHeight : 0;
        float stop = current == IconSwitch.Checked.LEFT ? 0 : scrollHeight;
        contentAnim.moveFromY(start).moveToY(stop).setDuration(400).start();
    }

    private void updateColors() {
        int colorIndex = iconSwitch.getChecked().ordinal();
        getWindow().setStatusBarColor(statusBarColors[colorIndex]);
        ShapeElement shape = new ShapeElement();
        shape.setRgbColor(RgbColor.fromArgbInt(toolbarColors[colorIndex]));
        title.setBackground(shape);
    }

    private void open(Uri uri) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction("android.intent.action.VIEW")
                .withUri(uri)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

}